//add map area panelId 
var dopBtnstodelAdd = "";
var mapManager = null;
var isModify = false;
$("#dop_datagrid_stodel").datagrid({
	url:"",
	height: 350,
	pagination:true,
	fitColumns:false,
	singleSelect:false,
	fit:true,
	pageSize: 10,
	pageList:[10,30,50,100],
	sortOrder:'desc',
	rownumbers:true,
	showFooter:true,
	frozenColumns:[[]],
	columns:[[
	    {field:'ck',checkbox:true},
		{field: "id", width: 150, title: "主键", hidden:true},
		{field: "restaurantId", width: 150, title: "店面名称", hidden:true},
		{field: "restaurantName", width: 150, title: "店面名称"},
		{field: "deliverymanId", width: 150, title: "员工姓名", hidden:true},
		{field: "deliverymanName", width: 150, title: "员工姓名"}
	]]
});

init();

//click pagination
var p = $("#dop_datagrid_stodel").datagrid('getPager');
$(p).pagination({
    onSelectPage: function (pageNumber, pageSize) {
    	var params = $.extend({pageNum:pageNumber, pageSize:pageSize}, getQueryParams());
		EapTools.doAction("stoDelController.do?queryList", params, false, function(data) {
			$("#dop_datagrid_stodel").datagrid('loadData', {"rows": data.list, "total": data.count}|| {});
		});
    }
});


//bind add event
$("#dop_btn_stodel_add").click(function() {
	isModify = false;
    //show map dialog
	dopBtnstodelAdd = jQuery.layer({
		type : 1,
		title : '<div><span style="padding-left: 14px;line-height: 14px;">添加店面</span></div>',
		shadeClose : true,
		maxmin : false,
		fix : false,
		area : [1000, 150 ],
		page : {
			dom : '#dop_stodel_add_panel'
		}
	});
});

//bind modify event
$("#dop_btn_stodel_modify").click(function() {
	var selections = $("#dop_datagrid_stodel").datagrid("getSelections");
	if (selections && selections.length > 0) {
		if (selections.length > 1) {
			layer.tips('请选择一行数据', this , {guide: 2, time: 2});
		} else {
             // 显示地图div
			$("#dop_btn_stodel_add").click();
			//获得此项记录的坐标字符串						   						    
			$("select[id='restaurantId']").val(selections[0].restaurantId);
			$("select[id='deliverymanId']").val(selections[0].deliverymanId);
			isModify = true;
		}
	} else
		layer.tips('请选择要修改行', this, {
			guide : 2,
			time : 2
		});
});

//bind delete event
$("#dop_btn_stodel_delete").click(function() {
	var selections = $("#dop_datagrid_stodel").datagrid("getSelections");
	if (selections && selections.length > 0)
		layer.confirm('确定删除吗？', function() {
			EapTools.doAction("stoDelController.do?delete", {"rowList" : JSON.stringify(selections)}, false, function(data) {
				if (data.success) {
					EapTip.msgOk("删除成功！");
					init();
				}
			});
		});
	else
		layer.tips('请选择要删除行', this, {
			guide : 2,
			time : 2
		});
});

//添加面板 确定按钮
$("#dop_stodel_add_panel_ok").click(function() {
	if (isModify)
		EapTools.doAction("stoDelController.do?modify", {restaurantId : $("select[id='restaurantId']").val(),
			deliverymanId : $("select[id='deliverymanId']").val(),
			id : $("#dop_datagrid_stodel").datagrid("getSelections")[0].id}, false, function(data) {
			layer.close(dopBtnstodelAdd);
			EapTip.msgOk("修改成功");
			$("select[code='restaurantId']").val("");
			$("select[code='deliverymanId']").val("");
			init();
		});
	else 
		EapTools.doAction("stoDelController.do?create", {restaurantId : $("select[id='restaurantId']").val(),
			deliverymanId : $("select[id='deliverymanId']").val()}, false, function(data) {
			layer.close(dopBtnstodelAdd);
			EapTip.msgOk("添加成功");
			$("select[code='restaurantId']").val("");
			$("select[code='deliverymanId']").val("");
			init();
		});
});

//添加面板 取消按钮
$("#dop_stodel_add_panel_cancel").click(function() {
	layer.close(dopBtnstodelAdd);
});

//bind query event
$("#dop_btn_stodel_query").click(function() {
	init();
});

//绑定店面列表
EapTools.doAction("storefrontController.do?findData", {}, false, function(data) {
	var selectorArray = [];
	selectorArray.push("<option value=''>---请选择---</option>");
	for (var i = 0; i < data.length; i++) {
		var thisObj = data[i];
		selectorArray.push("<option value='", thisObj.id, "'>", thisObj.stoName, "</option>");
	}
	$("select[code='restaurantId']").append(selectorArray.join(""));
	$("select[id='restaurantId']").append(selectorArray.join(""));
});

//绑定业务人员列表
EapTools.doAction("deliveryManController.do?findData", {}, false, function(data) {
	var selectorArray = [];
	selectorArray.push("<option value=''>---请选择---</option>");
	for (var i = 0; i < data.length; i++) {
		var thisObj = data[i];
		selectorArray.push("<option value='", thisObj.id, "'>", thisObj.delName, "</option>");
	}
	$("select[code='deliverymanId']").append(selectorArray.join(""));
	$("select[id='deliverymanId']").append(selectorArray.join(""));
});

//get params
function getQueryParams() {
	return {
		restaurantId : $("select[code='restaurantId']").val() || 0,
		deliverymanId : $("select[code='deliverymanId']").val() || 0
	};
}

//init
function init() {
	//init
	var options = $("#dop_datagrid_stodel").datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({pageNum : 1, pageSize : pageSize}, getQueryParams());
	EapTools.doAction("stoDelController.do?queryList", params,false, function(data) {
		$("#dop_datagrid_stodel").datagrid('loadData', {"rows" : data.list, "total" : data.count} || {});
		var p = $("#dop_datagrid_stodel").datagrid('getPager');
		$("td:last", p).find("a").click();
	});
}